<?php

namespace App\Http\Requests;

use App\Models\Category;
use Illuminate\Foundation\Http\FormRequest;

class ContentRequest extends FormRequest
{
    protected $fields;

    public function __construct()
    {
        $this->fields = Category::find(request('category_id'))->article->fields;
    }

    public function rules()
    {
        $rules = [];
        foreach ($this->fields as $field) {
            if ($field['validate_rule']) $rules[$field['name']] = $field['validate_rule'];
        }
        return $rules + [
                'title' => ['required', 'between:3,100'],
                'url' => ['sometimes', 'nullable', 'url'],
                'content' => ['sometimes', 'min:10'],
                ];
    }

    public function attributes()
    {
        $attributes = [];
        foreach ($this->fields as $field) {
            $attributes[$field['name']] = $field['title'];
        }
        return $attributes;
    }
}
